import wx
import wx.adv

TRAY_ICON = '../resources/computer.png'
TRAY_TOOLTIP = '托盘示例'

from src.pc_info2 import MyFrame  # 假设“我的计算机”程序代码保存在my_computer_module.py文件中
from src.task_aui_frame import task_aui_frame
from src.ai_chat import AiChatWindow


class MonitorTaskBarIcon(wx.adv.TaskBarIcon):
    def __init__(self, app):
        super().__init__()
        self.app = app
        icon = wx.Icon(TRAY_ICON)
        self.SetIcon(icon, TRAY_TOOLTIP)
        self.Bind(wx.adv.EVT_TASKBAR_RIGHT_DOWN, self.OnRightClicked)
        self.task_frame = None
        self.ai_chat_window = None
        self.pc_info_frame = None

    @staticmethod
    def create_menu_item(menu, label, func):
        item = wx.MenuItem(menu, -1, label)
        menu.Bind(wx.EVT_MENU, func, id=item.GetId())
        menu.Append(item)
        return item

    def on_task_aui(self, event):
        if not self.task_frame:
            self.task_frame = task_aui_frame(None, "任务管理器")
        self.task_frame.Show(True)

    def on_pc_info(self, event):
        if not self.pc_info_frame:
            self.pc_info_frame = MyFrame(None)
        self.pc_info_frame.Show(True)

    def on_ai_chat(self, event):
        if not self.ai_chat_window:
            self.ai_chat_window = AiChatWindow(None)
        self.ai_chat_window.Show(True)

    def OnRightClicked(self, event):
        menu = wx.Menu()
        self.create_menu_item(menu, "我的计算机", self.on_pc_info)
        self.create_menu_item(menu, "AI聊天", self.on_ai_chat)
        self.create_menu_item(menu, "任务管理器", self.on_task_aui)
        menu.AppendSeparator()
        menu.Append(wx.ID_EXIT, "E&xit")
        self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT)

        self.PopupMenu(menu)
        menu.Destroy()

    def OnExit(self, event):
        wx.GetApp().ExitMainLoop()


class MyApp(wx.App):
    def OnInit(self):
        frame = wx.Frame(None, wx.ID_ANY, "TaskBar")
        self.SetTopWindow(frame)
        self.task_bar_icon = MonitorTaskBarIcon(self)  # 保存 MonitorTaskBarIcon 实例
        return True


if __name__ == '__main__':
    app = MyApp()
    app.MainLoop()
